define([
    'jquery',
    'underscore',
    'backbone',
    'module/import/models/ImportModel',
    'module/import/models/ImportCollection',
    'module/import/views/List',
    'text!module/import/templates/main.html',
    'text!module/import/templates/add.html',
    'text!module/import/templates/edit.html',
    'text!module/import/templates/drop.html',
], function($, _, Backbone, ImportModel, ImportCollection, List, main, add, edit, drop) {

    var ImportController = Backbone.View.extend({
        el: $("#page"),
        initialize: function() {
            this.render();
            this.list_view();
        },
        events: {
            "click #list_view": "list_view",
            "click #add_view": "add_view",
            "click #edit_view": "edit_view",
            "click #drop_view": "drop_view",
            "click #add_action": "add_action",
            "click #edit_action": "edit_action",
            "click #drop_action": "drop_action",
            "click #search": "search",
            "click #pag": "pag_action"
        },
        render: function() {
            this.$el.html(main);
        },
        list_view: function() {
            $.ajax({
                url: 'server/api/import/find_all/1',
                type: 'GET',
                dataType: "json",
                success: function(data) {
                    var collection = new ImportCollection(data.result);
                    var list = new List({collection: collection, count: data.count});
                    list.render();
                }
            });
        },
        add_view: function() {
            $("#main-list").html(add);
            $('#FileUploader').on('submit', function(e)
            {
                e.preventDefault();
                $('#uploadButton').attr('disabled', ''); // disable upload button
                //show uploading message
                $("#output").html('<div style="padding:10px"><img src="images/ajax-loader.gif" alt="Please Wait"/> <span>Uploading...</span></div>');
                $(this).ajaxSubmit({
                    target: '#output',
                    success: afterSuccess //call function after success
                });
            });
        },
        edit_view: function(event) {
            $("#main-list").html(edit);
            var id = event.target.getAttribute('index');
            /*var import = new ImportModel({id: id});
             import.fetch({
             success: function(data) {
             data = data.toJSON()
             $("#id").val(data.id);
             $("#name").val(data.name);
             }
             });*/
        },
        drop_view: function(event) {
            $("#main-list").html(drop);
            var id = event.target.getAttribute('index');
            /*var import = new ImportModel({id: id});
             import.fetch({
             success: function(data) {
             data = data.toJSON()
             $("#id").val(data.id);
             $("#name").val(data.name);
             }
             });*/
        },
        add_action: function() {
            var self = this;
            /* import = new ImportModel();
             var importDetails = {
             name: $("#name").val()
             };
             import.save(importDetails, {
             success: function(data) {
             self.list_view();
             }
             });*/
        },
        edit_action: function() {
            var self = this;
            /*var import = new ImportModel();
             var importDetails = {
             id: $("#id").val(),
             name: $("#name").val()
             };
             import.save(importDetails, {
             success: function(data) {
             self.list_view();
             }
             });*/
        },
        drop_action: function() {
            var self = this;
            /*var import = new ImportModel({
             id: $("#id").val()
             });
             import.destroy({
             success: function(data) {
             self.list_view();
             }
             });*/
        },
        search: function() {
            var search = $('#txt_search').val();
            $.ajax({
                url: 'server/api/import/search/' + search,
                type: 'GET',
                dataType: "json",
                success: function(data) {
                    var collection = new ImportCollection(data.result);
                    var list = new List({collection: collection, count: data.count});
                    list.render();
                }
            });
        },
        pag_action: function(event) {
            var id = event.target.getAttribute('index');
            $.ajax({
                url: 'server/api/import/find_all/' + id,
                type: 'GET',
                dataType: "json",
                success: function(data) {
                    var collection = new ImportCollection(data.result);
                    var list = new List({collection: collection, count: data.count});
                    list.render();
                }
            });
        }
    });
    return ImportController;
});

/*upload: function() {
 
 }*/

function afterSuccess()
{
    $('#FileUploader').resetForm(); // reset form
    $('#uploadButton').removeAttr('disabled'); //enable submit button
}